#pragma once
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<sys/types.h>
#include<unistd.h>

#define TaskNum 3
typedef void (*task_t)();//task_t函数指针类型
using namespace std;

task_t tasks[TaskNum];
void Print()
{
    cout<<"I am Print Task"<<endl;
}
void Downland()
{
    cout<<"I am Downland Task"<<endl;
}
void Flush()
{
    cout<<"I am Flush Task"<<endl;
}
void LoadTask()
{
    srand(time(nullptr)^getpid());
    tasks[0]=Print;
    tasks[1]=Downland;
    tasks[2]=Flush;
}
void ExcuteTask(int num)
{
    if(num<0||num>2) return;
    tasks[num]();
}
int SelectTask()
{
    return rand()%TaskNum;
}
void work()
{
    while(true)
    {

        int command=0;
        int n=read(0,&command,sizeof(command));
        if(n==sizeof(int))
        {
            cout<<"pid is: "<<getpid()<<"handler task "<<endl;
            ExcuteTask(command);
        }
        else if(n==0)
        {
            cout<<"sub process: "<<getpid()<<" quit "<<endl;
            break;
        }
    }
}